package com.lagou.page.controller;

import com.lagou.commen.pojo.Products;
import com.lagou.page.feign.ProductFeign;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 李树飞
 */
@RestController
@RequestMapping("/page")
public class PageController {

    /*@Autowired
    private RestTemplate restTemplate;

    //服务注册中心客户端对象
    @Autowired
    private DiscoveryClient discoveryClient;*/

    @Autowired
    private ProductFeign productFeign;

    @GetMapping("/getProduct/{id}")
    public Products findDataById(@PathVariable Integer id) {
       /* List<ServiceInstance> instances = discoveryClient.getInstances("lagou-service-product");
        ServiceInstance instance = instances.get(0);
        String host = instance.getHost();
        int port = instance.getPort();
        String url = "http://" + host + ":" + port + "/product/query/" + id;
        return restTemplate.getForObject(url, Products.class);*/
       return productFeign.findById(id);
    }

    @GetMapping("/getProductServerPort")
    public String getProductServerPort() {
        /*String url = "http://lagou-service-product/service/port/";

        return restTemplate.getForObject(url, String.class);*/

        return productFeign.getPort();

    }

    @HystrixCommand(
            threadPoolKey = "getProductServerPort2",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "1"), // 线程数
                    @HystrixProperty(name = "maxQueueSize", value = "20") // 等待队列长
            },
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
            }
    )
    @GetMapping("/getProductServerPort2")
    public String getProductServerPort2() {
        /*String url = "http://lagou-service-product/service/port/";

        return restTemplate.getForObject(url, String.class);*/
        return productFeign.getPort();
    }

    @HystrixCommand(
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
                    //统计窗口时间的设置
                    @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds",value = "8000"),
                    //统计窗口内的最小请求数
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "2"),
                    //统计窗口内错误请求阈值的设置 50%
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "50"),
                    //自我修复的活动窗口时间
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "3000")

            },
            fallbackMethod = "myFallBack"
    )
    @GetMapping("/getProductServerPort3")
    public String getProductServerPort3() {
        /*String url = "http://lagou-service-product/service/port/";

        return restTemplate.getForObject(url, String.class);*/
        return productFeign.getPort();
    }

    public String myFallBack() {
        return "-1"; // 兜底数据
    }
}
